package srpingmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class HelloControllerThree {

    //    使用 Servlet 原生 API 实现
    @RequestMapping("/requestParam")
    public ModelAndView getParam(@RequestParam("userName") String userName, @RequestParam("password") String password, HttpServletRequest request,
                                 HttpServletResponse response) {
        System.out.println("Spring RequestParam 传参方式");
        System.out.println(userName);
        System.out.println(password);
        return null;
    }

    //    客户端跳转
    @RequestMapping("/jump")
    public ModelAndView jump() {
        ModelAndView mav = new ModelAndView("redirect:/hellospringmvc");
        return mav;
    }

    @RequestMapping(value = "/indexException")
    @ExceptionHandler({SpringException.class})
    public String index() {
        if (true) {
            throw new SpringException("Given name is too short");
        }
        return "index";
    }

    //    客户端跳转
    @RequestMapping("/checksession")
    public ModelAndView check(HttpSession session) throws Exception {
        Integer i= (Integer) session.getAttribute("count");
        if (i==null) {
            i=0;
        }
        i++;
        session.setAttribute("count",i);
        ModelAndView mav = new ModelAndView("check");
        mav.addObject("message", "Hello Spring MVC");
        return mav;
    }
}
